/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example;

import jsc.datastructures.GroupedData;
import jsc.independentsamples.OneWayANOVA;

/**
 *
 * @author Nadeeshani Hewage
 * Undergraduate: University of Moratuwa, Sri Lanka
 */
public class OneWayAnovaUnstacked {
    String labels[];
    String treatmentList[];
    double observations[];
    GroupedData core;
    OneWayANOVA tester;
    String[][] table;

    double treatmentDOF, errorDOF, totalDOF;        // DOF stands for Degrees of Freedom
    double treatmentSS, errorSS, totalSS;           // SS stands for Sum of Squares
    double treatmentMS, errorMS;                    // MS stands for Mean Square
    double fRatio;
    int noOfVar;

    public OneWayAnovaUnstacked(String[][] treatments, double[][] responseVariable){

        noOfVar = (responseVariable.length)*(responseVariable[0].length);//reads total size of input data range

        treatmentList=new String[treatments[0].length];                  //creates and inserts cell data of treatment labels
                                                                         //from the spreadsheet to the string array
            for(int i=0;i<treatments[0].length;i++){
            treatmentList[i]=treatments[0][i];
        }

        labels=new String[noOfVar];                                     //String array that is ready for grouped data structure
        observations=new double[noOfVar];                               //double array that is ready for grouped data structure

        int x=0;                                                        //counter for grouped data input
                                                                        //variables treatmentList &observations
        for(int i=0;i< responseVariable[0].length;i++){
            for(int j=0;j<responseVariable.length;j++){
                observations[x]=responseVariable[j][i];
                if(x==0){
                    labels[x]=treatmentList[0];                         //arranging labels as required by the data structure
                }else{
                    labels[x]=treatmentList[x/(responseVariable.length)];
                }
                x++;
            }
        }
        core=new GroupedData(observations,labels);                      //GroupedData datastructure instance
        tester=new OneWayANOVA(core);                                   //Anova instance
    }


   public String[][] OneWayTable(){                                     //required values to create the Analysis of variance table

        treatmentDOF=(tester.getTreatmentCount()-1);
        errorDOF    =(tester.getN()-tester.getTreatmentCount());
        totalDOF    =(tester.getN()-1);
        //Sums of squares: treatmentSS, errorSS, totalSS;
        treatmentSS =tester.getEss();
        errorSS     =tester.getRss();
        totalSS     =tester.getTss();
        //mean squares: double treatmentMS, errorMS;
        treatmentMS =tester.getEms();
        errorMS     =tester.getRms();
        //fCalculated
        fRatio      =tester.getTestStatistic();

        String StreatmentDOF, SerrorDOF, StotalDOF;                  // DOF stands for Degrees of Freedom
        String StreatmentSS, SerrorSS, StotalSS;                     // SS stands for Sum of Squares
        String StreatmentMS, SerrorMS;                               // MS stands for Mean Square
        String SfRatio,statem;

        StreatmentDOF=""+treatmentDOF;                               //concatenation of strings
        SerrorDOF=""+errorDOF;
        StotalDOF=""+totalDOF;
        StreatmentSS=""+treatmentSS;
        SerrorSS=""+errorSS;
        StotalSS=""+totalSS;
        StreatmentMS=""+treatmentMS;
        SerrorMS=""+errorMS;
        SfRatio=""+fRatio;
        statem= "Your data supports null hypothesis upto "+tester.getSP()+"significance level";
        table=new String[][]{{"Null Hypothesis: Means of all treatments are equal"},        //output table
                             {"Source","DOF","SS","MS","F-ratio"},
                             {"Treatment",StreatmentDOF,StreatmentSS,StreatmentMS,SfRatio},
                             {"Error",SerrorDOF,SerrorSS,SerrorMS,""},
                             {"Total",StotalDOF,StotalSS,"",""},
                             {statem}};

        return table;
    }

}
